/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.report;

import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.entity.ServiceGroup;
import com.thienloc.omap.session.RequestQueueFacade;
import com.thienloc.omap.session.RequestQueueHistFacade;
import com.thienloc.omap.session.ServiceGroupFacade;
import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class ProductionByServiceBean extends BaseBean implements Serializable {

    @EJB
    private RequestQueueFacade requestQueueFacade;
    @EJB
    private RequestQueueHistFacade requestQueueHistFacade;
    @EJB
    private ServiceGroupFacade serviceGroupFacade;
    private Date day;
    private String month = "8";
    private String year = "2012";
    //khung ngày
    private String startMonth;
    private String startYear;
    private String endMonth;
    private String endYear;
    private Integer startDay;
    private Integer endDay;
    //khung giờ
    private Date startDate;
    private Date endDate;
    private Integer startHour;
    private Integer endHour;
    private String reportType;
    private final String BY_MONTH = "byMonth";
    private final String BY_DAY = "byDay";
    private final String BY_DAY_RANGE = "byDayRange";
    private final String BY_HOUR_RANGE = "byHourRange";
    private List<ProductionOfRequestDetail> serviceGroupDetailList;
    private List<String> selectedDeliverProvider;
    private Map<String, ProductionOfTime> timeMap;
    private final Logger logger = LoggerFactory.getLogger(ProductionByServiceBean.class);

    @PostConstruct
    public void init() {
        timeMap = new TreeMap<String, ProductionOfTime>();
    }

    private void createDataModel(List records) throws Exception {

        List<ServiceGroup> allServiceGroups = serviceGroupFacade.findAll();
        Map<String, ServiceGroup> allServiceGroupMap = new HashMap<String, ServiceGroup>();
        for (ServiceGroup serviceGroup : allServiceGroups) {
            allServiceGroupMap.put(serviceGroup.getRequestDetail(), serviceGroup);
        }

        for (Object row : records) {
            Object[] fields = (Object[]) row;
            String requestDetail = (String) fields[2];
            Integer production = ((BigDecimal) fields[0]).intValue();
            String time = (String) fields[1];

            String refinedRequestDetail = refineRequestDetail(requestDetail);
            if (refinedRequestDetail == null) {
                logger.warn("Cannot refine...");
            } else {
                ServiceGroup serviceGroup = allServiceGroupMap.get(refinedRequestDetail);

                if (serviceGroup != null) {
                    logger.debug("[{}] map to service group: [{}]", requestDetail, serviceGroup.getServiceGroupName());
                    ProductionOfTime pot = timeMap.get(time);

                    if (pot != null) {
                        pot.addProduction(serviceGroup.getServiceGroupName(), refinedRequestDetail, production);
                    } else {
                        pot = new ProductionOfTime(time);
                        pot.addProduction(serviceGroup.getServiceGroupName(), refinedRequestDetail, production);
                        timeMap.put(time, pot);
                    }
                } else {
                    logger.warn("{}-{} Cannot be mapped to any service group, production:", ((BigDecimal) fields[0]).intValue(), requestDetail);
                }
            }
        }
    }

    private String refineRequestDetail(String requestDetail) {
        if (requestDetail != null && !requestDetail.trim().isEmpty()) {
            String[] chunks = requestDetail.split(" ");
            StringBuilder sb = new StringBuilder();

            int chunkCount = 0;
            for (String chunk : chunks) {
                if (!chunk.trim().isEmpty()) {
                    sb.append(chunk.trim());
                    sb.append(" ");
                    chunkCount++;
                }
                if (chunkCount == 2) {
                    break;
                }
            }
            sb.delete(sb.lastIndexOf(" "), sb.length());
            logger.debug("[{}] refined to [{}]", requestDetail, sb.toString());
            return sb.toString();
        }
        logger.error("Request detail is null: {}", requestDetail);
        return null;
    }

    public List<ProductionOfTime> getDataModel() {
        return new ArrayList<ProductionOfTime>(timeMap.values());
    }

    public List<ReportModel> getSingleReportModel() {
        List<ProductionOfTime> productionOfTimes = getDataModel();
        List<ReportModel> result = new ArrayList<ReportModel>(productionOfTimes.size());
        for (ProductionOfTime productionOfTime : productionOfTimes) {

            ReportModel reportModel = new ReportModel();
            String date = productionOfTime.getTime();
            reportModel.setDate(date);

            List<ProductionOfServiceGroup> productionOfServiceGroups = productionOfTime.getProductionOfServiceGroups();
            for (ProductionOfServiceGroup productionOfServiceGroup : productionOfServiceGroups) {
                String serviceGroup = productionOfServiceGroup.getServiceGroup();
                if (serviceGroup.equals("S2")) {
                    reportModel.setS2Production(productionOfServiceGroup.getTotal());
                } else if (serviceGroup.equals("BASIC")) {
                    reportModel.setBaseServiceProduction(productionOfServiceGroup.getTotal());
                } else if (serviceGroup.equals("TXTBASE")) {
                    reportModel.setTxtBaseProduction(productionOfServiceGroup.getTotal());
                }
            }

            result.add(reportModel);
        }

        return result;
    }

    public List<MultipleReportModel> getReportByDayRangeModel() {
        List<ProductionOfTime> productionOfTimes = getDataModel();

        Map<String, MultipleReportModel> reportListMap = new HashMap<String, MultipleReportModel>();

        for (ProductionOfTime productionOfTime : productionOfTimes) {
            String date = productionOfTime.getTime();
            String strMonth = date.substring(date.indexOf('-'));

            MultipleReportModel multipleReportModel = reportListMap.get(strMonth);
            if (multipleReportModel == null) {
                multipleReportModel = new MultipleReportModel();
                reportListMap.put(strMonth, multipleReportModel);
            }

            ReportModel reportModel = new ReportModel();
            reportModel.setDate(date);

            List<ProductionOfServiceGroup> productionOfServiceGroups = productionOfTime.getProductionOfServiceGroups();
            for (ProductionOfServiceGroup productionOfServiceGroup : productionOfServiceGroups) {
                String serviceGroup = productionOfServiceGroup.getServiceGroup();
                if (serviceGroup.equals("S2")) {
                    reportModel.setS2Production(productionOfServiceGroup.getTotal());
                } else if (serviceGroup.equals("BASIC")) {
                    reportModel.setBaseServiceProduction(productionOfServiceGroup.getTotal());
                } else if (serviceGroup.equals("TXTBASE")) {
                    reportModel.setTxtBaseProduction(productionOfServiceGroup.getTotal());
                }
            }

            multipleReportModel.addReportModel(reportModel);
        }

        return new ArrayList<MultipleReportModel>(reportListMap.values());
    }
    
    public List<MultipleReportModel> getReportByHourRangeModel() {
        List<ProductionOfTime> productionOfTimes = getDataModel();

        Map<String, MultipleReportModel> reportListMap = new HashMap<String, MultipleReportModel>();

        for (ProductionOfTime productionOfTime : productionOfTimes) {
            String date = productionOfTime.getTime();
            logger.debug(date);
            String strMonth = date.substring(date.indexOf(' '));

            MultipleReportModel multipleReportModel = reportListMap.get(strMonth);
            if (multipleReportModel == null) {
                multipleReportModel = new MultipleReportModel();
                reportListMap.put(strMonth, multipleReportModel);
            }

            ReportModel reportModel = new ReportModel();
            reportModel.setDate(date);

            List<ProductionOfServiceGroup> productionOfServiceGroups = productionOfTime.getProductionOfServiceGroups();
            for (ProductionOfServiceGroup productionOfServiceGroup : productionOfServiceGroups) {
                String serviceGroup = productionOfServiceGroup.getServiceGroup();
                if (serviceGroup.equals("S2")) {
                    reportModel.setS2Production(productionOfServiceGroup.getTotal());
                } else if (serviceGroup.equals("BASIC")) {
                    reportModel.setBaseServiceProduction(productionOfServiceGroup.getTotal());
                } else if (serviceGroup.equals("TXTBASE")) {
                    reportModel.setTxtBaseProduction(productionOfServiceGroup.getTotal());
                }
            }

            multipleReportModel.addReportModel(reportModel);
        }

        return new ArrayList<MultipleReportModel>(reportListMap.values());
    }

    public void reportByMonth() {
        try {
            timeMap = new TreeMap<String, ProductionOfTime>();
            Calendar cStartDate = Calendar.getInstance();
            cStartDate.set(Calendar.YEAR, Integer.parseInt(year));
            cStartDate.set(Calendar.MONTH, Integer.parseInt(month) - 1);
            cStartDate.set(Calendar.DATE, 1);
            cStartDate.set(Calendar.HOUR_OF_DAY, 0);
            cStartDate.set(Calendar.MINUTE, 0);
            cStartDate.set(Calendar.SECOND, 0);

            Calendar cEndDate = Calendar.getInstance();
            cEndDate.set(Calendar.YEAR, Integer.parseInt(year));
            cEndDate.set(Calendar.MONTH, Integer.parseInt(month));// tháng sau
            cEndDate.set(Calendar.DATE, 1);
            cEndDate.add(Calendar.DATE, -1);//lùi 1 ngày=> đc ngày cuối cùng của tháng này
            cEndDate.set(Calendar.HOUR_OF_DAY, 23);
            cEndDate.set(Calendar.MINUTE, 59);
            cEndDate.set(Calendar.SECOND, 59);

            Date[] dateRange = new Date[]{cStartDate.getTime(), cEndDate.getTime()};
            List<Date[]> dateRanges = new ArrayList<Date[]>(1);
            dateRanges.add(dateRange);

            List list = requestQueueFacade.countProductionOfService(selectedDeliverProvider, "dd-mm-yyyy", dateRanges);
            List historyList = requestQueueHistFacade.countProductionOfService(selectedDeliverProvider, "dd-mm-yyyy", dateRanges);
            createDataModel(list);
            createDataModel(historyList);

//            for (ProductionOfOperator productionOfOperator : operatorMap.values()) {
//                for (ProductionOfTime productionOfTime : productionOfOperator.getProductionOfTimes()) {
//                    try {
//                        String time = productionOfTime.getTime();
//                        String hour = time.substring(0, time.indexOf("-")).trim();
//                        productionOfTime.setTime(hour);
//                    } catch (Exception e) {
//                        logger.error("Error Parsing date {}", productionOfTime.getTime(), e);
//                    }
//                }
//            }
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void reportByDay() {
        try {
            timeMap = new TreeMap<String, ProductionOfTime>();
            Calendar cStartDate = Calendar.getInstance();
            cStartDate.setTime(day);
            cStartDate.set(Calendar.HOUR_OF_DAY, 0);
            cStartDate.set(Calendar.MINUTE, 0);
            cStartDate.set(Calendar.SECOND, 0);

            Calendar cEndDate = Calendar.getInstance();
            cEndDate.setTime(day);
            cEndDate.set(Calendar.HOUR_OF_DAY, 23);
            cEndDate.set(Calendar.MINUTE, 59);
            cEndDate.set(Calendar.SECOND, 59);

            Date[] dateRange = new Date[]{cStartDate.getTime(), cEndDate.getTime()};
            List<Date[]> dateRanges = new ArrayList<Date[]>(1);
            dateRanges.add(dateRange);

            List list = requestQueueFacade.countProductionOfService(selectedDeliverProvider, "dd-mm-yyyy hh24", dateRanges);
            List historyList = requestQueueHistFacade.countProductionOfService(selectedDeliverProvider, "dd-mm-yyyy hh24", dateRanges);
            createDataModel(list);
            createDataModel(historyList);

//            for (ProductionOfOperator productionOfOperator : operatorMap.values()) {
//                for (ProductionOfTime productionOfTime : productionOfOperator.getProductionOfTimes()) {
//                    String time = productionOfTime.getTime();
//                    String hour = time.substring(time.lastIndexOf(" ")).trim();
//                    productionOfTime.setTime(hour);
//                }
//            }
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void reportByDayRange() {
        try {
            timeMap = new TreeMap<String, ProductionOfTime>();
            Calendar outterStartDate = Calendar.getInstance();
            outterStartDate.set(Calendar.YEAR, Integer.parseInt(startYear));
            outterStartDate.set(Calendar.MONTH, Integer.parseInt(startMonth) - 1);
            outterStartDate.set(Calendar.DATE, 1);
            outterStartDate.set(Calendar.HOUR_OF_DAY, 0);
            outterStartDate.set(Calendar.MINUTE, 0);
            outterStartDate.set(Calendar.SECOND, 0);

            Calendar outterEndDate = Calendar.getInstance();
            outterEndDate.set(Calendar.YEAR, Integer.parseInt(endYear));
            outterEndDate.set(Calendar.MONTH, Integer.parseInt(endMonth) - 1);// tháng sau
            outterEndDate.set(Calendar.DATE, 1);
            outterEndDate.set(Calendar.HOUR_OF_DAY, 23);
            outterEndDate.set(Calendar.MINUTE, 59);
            outterEndDate.set(Calendar.SECOND, 59);

            List<Date[]> dateRanges = new ArrayList<Date[]>();
            do {
                Calendar innerStartDate = Calendar.getInstance();
                innerStartDate.setTime(outterStartDate.getTime());
                Calendar innerEndDate = Calendar.getInstance();
                innerEndDate.setTime(outterStartDate.getTime());
                for (int iDay = startDay; iDay < endDay + 1; iDay++) {
                    innerStartDate.set(Calendar.DATE, iDay);
                    innerStartDate.set(Calendar.HOUR_OF_DAY, 0);
                    innerStartDate.set(Calendar.MINUTE, 0);
                    innerStartDate.set(Calendar.SECOND, 0);

                    innerEndDate.set(Calendar.DATE, iDay);
                    innerEndDate.set(Calendar.HOUR_OF_DAY, 23);
                    innerEndDate.set(Calendar.MINUTE, 59);
                    innerEndDate.set(Calendar.SECOND, 59);

                    Date[] dateRange = new Date[]{innerStartDate.getTime(), innerEndDate.getTime()};
                    dateRanges.add(dateRange);
                }

                outterStartDate.add(Calendar.MONTH, 1);
            } while (outterStartDate.before(outterEndDate));

            List list = requestQueueFacade.countProductionOfService(selectedDeliverProvider, "dd-mm-yyyy", dateRanges);
            List historyList = requestQueueHistFacade.countProductionOfService(selectedDeliverProvider, "dd-mm-yyyy", dateRanges);
            createDataModel(list);
            createDataModel(historyList);

        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void reportByHourRange() {
        try {
            timeMap = new TreeMap<String, ProductionOfTime>();
            Calendar outterStartDate = Calendar.getInstance();
            outterStartDate.setTime(startDate);
            outterStartDate.set(Calendar.HOUR_OF_DAY, 0);
            outterStartDate.set(Calendar.MINUTE, 0);
            outterStartDate.set(Calendar.SECOND, 0);

            Calendar outterEndDate = Calendar.getInstance();
            outterEndDate.setTime(endDate);
            outterEndDate.set(Calendar.HOUR_OF_DAY, 23);
            outterEndDate.set(Calendar.MINUTE, 59);
            outterEndDate.set(Calendar.SECOND, 59);

            List<Date[]> dateRanges = new ArrayList<Date[]>();
            do {
                Calendar innerStartDate = Calendar.getInstance();
                innerStartDate.setTime(outterStartDate.getTime());
                Calendar innerEndDate = Calendar.getInstance();
                innerEndDate.setTime(outterStartDate.getTime());
                for (int iHour = startHour; iHour < endHour + 1; iHour++) {
                    innerStartDate.set(Calendar.HOUR_OF_DAY, iHour);
                    innerStartDate.set(Calendar.MINUTE, 0);
                    innerStartDate.set(Calendar.SECOND, 0);

                    innerEndDate.set(Calendar.HOUR_OF_DAY, iHour);
                    innerEndDate.set(Calendar.MINUTE, 59);
                    innerEndDate.set(Calendar.SECOND, 59);

                    Date[] dateRange = new Date[]{innerStartDate.getTime(), innerEndDate.getTime()};
                    dateRanges.add(dateRange);
                }

                outterStartDate.add(Calendar.DATE, 1);
            } while (outterStartDate.before(outterEndDate));

            List list = requestQueueFacade.countProductionOfService(selectedDeliverProvider, "dd-mm-yyyy hh24", dateRanges);
            List historyList = requestQueueHistFacade.countProductionOfService(selectedDeliverProvider, "dd-mm-yyyy hh24", dateRanges);
            createDataModel(list);
            createDataModel(historyList);
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void viewServiceGroupDetail(String time, String serviceGroup) {
        logger.trace("Time: {} | Group: {}", time, serviceGroup);
        serviceGroupDetailList = timeMap.get(time).serviceGroupMap.get(serviceGroup).getProductionOfRequestDetails();
        for (ProductionOfRequestDetail productionOfRequestDetail : serviceGroupDetailList) {
            logger.debug("{}, {}", productionOfRequestDetail.getRequestDetail(), productionOfRequestDetail.getProduction());
        }
    }

    public Date getDay() {
        return day;
    }

    public void setDay(Date day) {
        this.day = day;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public List<String> getSelectedDeliverProvider() {
        return selectedDeliverProvider;
    }

    public void setSelectedDeliverProvider(List<String> selectedDeliverProvider) {
        this.selectedDeliverProvider = selectedDeliverProvider;
    }

    public String getStartMonth() {
        return startMonth;
    }

    public void setStartMonth(String startMonth) {
        this.startMonth = startMonth;
    }

    public String getStartYear() {
        return startYear;
    }

    public void setStartYear(String startYear) {
        this.startYear = startYear;
    }

    public String getEndMonth() {
        return endMonth;
    }

    public void setEndMonth(String endMonth) {
        this.endMonth = endMonth;
    }

    public String getEndYear() {
        return endYear;
    }

    public void setEndYear(String endYear) {
        this.endYear = endYear;
    }

    public Integer getStartDay() {
        return startDay;
    }

    public void setStartDay(Integer startDay) {
        this.startDay = startDay;
    }

    public Integer getEndDay() {
        return endDay;
    }

    public void setEndDay(Integer endDay) {
        this.endDay = endDay;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Integer getStartHour() {
        return startHour;
    }

    public void setStartHour(Integer startHour) {
        this.startHour = startHour;
    }

    public Integer getEndHour() {
        return endHour;
    }

    public void setEndHour(Integer endHour) {
        this.endHour = endHour;
    }

    public String getReportType() {
        return reportType;
    }

    public void setReportType(String reportType) {
        this.reportType = reportType;
    }

    public String getBY_MONTH() {
        return BY_MONTH;
    }

    public String getBY_DAY() {
        return BY_DAY;
    }

    public String getBY_DAY_RANGE() {
        return BY_DAY_RANGE;
    }

    public String getBY_HOUR_RANGE() {
        return BY_HOUR_RANGE;
    }

    public class MultipleReportModel {

        private List<ReportModel> reportModels = new ArrayList<ReportModel>();

        public List<ReportModel> getReportModels() {
            return reportModels;
        }

        public void setReportModels(List<ReportModel> reportModels) {
            this.reportModels = reportModels;
        }
        
        public void addReportModel(ReportModel reportModel){
            reportModels.add(reportModel);
        }
    }

    public class ReportModel {

        private String date;
        private Integer s2Production;
        private Integer baseServiceProduction;
        private Integer txtBaseProduction;

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public Integer getS2Production() {
            return s2Production;
        }

        public void setS2Production(Integer s2Production) {
            this.s2Production = s2Production;
        }

        public Integer getBaseServiceProduction() {
            return baseServiceProduction;
        }

        public void setBaseServiceProduction(Integer baseServiceProduction) {
            this.baseServiceProduction = baseServiceProduction;
        }

        public Integer getTxtBaseProduction() {
            return txtBaseProduction;
        }

        public void setTxtBaseProduction(Integer txtBaseProduction) {
            this.txtBaseProduction = txtBaseProduction;
        }
    }

    public List<ProductionOfRequestDetail> getServiceGroupDetailList() {
        return serviceGroupDetailList;
    }

    public void setServiceGroupDetailList(List<ProductionOfRequestDetail> serviceGroupDetailList) {
        this.serviceGroupDetailList = serviceGroupDetailList;
    }

    public class ProductionOfRequestDetail {

        private String requestDetail;
        private Integer production;

        public ProductionOfRequestDetail(String requestDetail, Integer production) {
            this.requestDetail = requestDetail;
            this.production = production;
        }

        public ProductionOfRequestDetail(String requestDetail) {
            this.requestDetail = requestDetail;
            this.production = 0;
        }

        public String getRequestDetail() {
            return requestDetail;
        }

        public void setRequestDetail(String requestDetail) {
            this.requestDetail = requestDetail;
        }

        public Integer getProduction() {
            return production;
        }

        public void setProduction(Integer production) {
            this.production = production;
        }
    }

    public class ProductionOfServiceGroup {

        private String serviceGroup;
        private Map<String, ProductionOfRequestDetail> requestDetailMap;

        public ProductionOfServiceGroup(String serviceGroup) {
            this.serviceGroup = serviceGroup;
            requestDetailMap = new TreeMap<String, ProductionOfRequestDetail>();
        }

        public String getServiceGroup() {
            return serviceGroup;
        }

        public void setServiceGroup(String serviceGroup) {
            this.serviceGroup = serviceGroup;
        }

        public List<ProductionOfRequestDetail> getProductionOfRequestDetails() {
            return new ArrayList<ProductionOfRequestDetail>(requestDetailMap.values());
        }

        public void addProduction(String requestDetail, Integer production) {
            ProductionOfRequestDetail pord = requestDetailMap.get(requestDetail);
            if (pord != null) {
                pord.setProduction(pord.getProduction() + production);
            } else {
                pord = new ProductionOfRequestDetail(requestDetail, production);
                requestDetailMap.put(requestDetail, pord);
            }
        }

        public Integer getTotal() {
            Integer total = 0;
            for (ProductionOfRequestDetail productionOfRequestDetail : requestDetailMap.values()) {
                total += productionOfRequestDetail.getProduction();
            }
            return total;
        }
    }

    public class ProductionOfTime {

        private String time;
        private Map<String, ProductionOfServiceGroup> serviceGroupMap;

        public ProductionOfTime(String time) {
            this.time = time;
            serviceGroupMap = new TreeMap<String, ProductionOfServiceGroup>();
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public List<ProductionOfServiceGroup> getProductionOfServiceGroups() {
            return new ArrayList<ProductionOfServiceGroup>(serviceGroupMap.values());
        }

        public void addProduction(String serviceGroup, String requestDetail, Integer production) {
            ProductionOfServiceGroup posg = serviceGroupMap.get(serviceGroup);
            if (posg != null) {
                posg.addProduction(requestDetail, production);
            } else {
                posg = new ProductionOfServiceGroup(serviceGroup);
                posg.addProduction(requestDetail, production);
                serviceGroupMap.put(serviceGroup, posg);
            }
        }

        public Integer getTotal() {
            Integer total = 0;
            for (ProductionOfServiceGroup productionOfServiceGroup : serviceGroupMap.values()) {
                total += productionOfServiceGroup.getTotal();
            }
            return total;
        }
    }
}
